home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 49 / Amiga Format CD49 (2000-01-17)(Future Publishing)(GB)(Track 1 of 3)[!][issue 2000-02].iso / -in_the_mag- / program_perfection / exit.c < prev    next >
C/C++ Source or Header  |  1999-12-08  |  3KB  |  107 lines

  1. /*
  2.  * exit.c
  3.  *
  4.  * Exit and error reporting stuff
  5.  *
  6.  * $Id :$
  7.  * $Log:$
  8.  *
  9.  */
  10.  
  11. #include "defs.h"
  12. #include "exit.h"
  13. #include <stdarg.h>
  14. #include <stdlib.h>
  15. #include <dos/dos.h>
  16. #include <dos/dosextens.h>
  17. #include <intuition/intuition.h>
  18. #include <proto/dos.h>
  19. #include <proto/exec.h>
  20. #include <proto/intuition.h>
  21.  
  22.  
  23. /*******************************************************************/
  24.  
  25. /*
  26.  * MyExit()
  27.  *
  28.  * General exit routine
  29.  *
  30.  * INPUTS:  err_message - if we wish to report an error, this
  31.  *          is a pointer to the text. It supports Printf() style
  32.  *          formatting.
  33.  *
  34.  * NOTES: If the program is being run from the WB, the message will
  35.  * be displayed in a standard requester; if from DOS, it gets dumped
  36.  * to the error stream.
  37.  *
  38.  * If a message is supplied, the program will exit via the standard
  39.  * exit() call with a RETURN_FAIL code; otherwise RETURN_OK.
  40.  *
  41.  * If you want to report an error code, set this via SetIoErr() before
  42.  * calling this routine.
  43.  */
  44.  
  45.  
  46. VOID
  47. MyExit( const STRPTR err_message, ... )
  48. {
  49.  
  50.     /* was there an error */
  51.     if( err_message )
  52.     {
  53.         /* better report it then */
  54.         va_list va;
  55.  
  56.         /* init pointer to arg list */
  57.         va_start( va, err_message );
  58.  
  59.         /* Were we started from the WB? */
  60.         if(WBMSG)
  61.         {
  62.             /* Yes. Show err_message in a requester */
  63.             struct EasyStruct err_req = { sizeof(struct EasyStruct),
  64.                         0,                  /* Flags */
  65.                         "Error",            /* Requester title */
  66.                         NULL,               /* Body text. Should be: err_message. See below */
  67.                         "Abort" };          /* Gadgets */
  68.  
  69.             /* VBCC won't do compound initilization with non constants */
  70.             /* and it doesn't believe that err_message is a constant. :-( */
  71.             /* So we'll fill in body text manually */
  72.             err_req.es_TextFormat = err_message;
  73.  
  74.             /* Put up the requester. */
  75.             /* We ignore the result since we only have one gadget and there's no */
  76.             /* way of finding out if intuition couldn't create the requester */
  77.             EasyRequestArgs( NULL, &err_req, NULL, va );
  78.         }
  79.         else
  80.         {
  81.             /* We were run from CLI - dump to error stream */
  82.             BPTR StdErr;
  83.  
  84.             /* Try to get a handle on the default error stream */
  85.             {
  86.                 /* find ourselves ;-) */
  87.                 struct Process *this_task = (struct Process *) FindTask( NULL );
  88.  
  89.                 /* Do we have an error stream? */
  90.                 /* If not use the default output stream */
  91.                 StdErr = ( this_task->pr_CES ) ? this_task->pr_CES : this_task->pr_COS;
  92.             }
  93.  
  94.             VFPrintf( StdErr, err_message, va );
  95.             FPutC( StdErr, '\n' );
  96.         }
  97.  
  98.         /* finished with args */
  99.         va_end(va);
  100.     }
  101.  
  102.     exit( err_message ? RETURN_FAIL : RETURN_OK );
  103. }
  104.  
  105.  
  106. /*******************************************************************/
  107.